package cl.mt.data;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Collection;
import java.util.LinkedList;

import cl.mt.dto.DTOParametros;
import cl.mt.util.bd.DatosTabla;
import cl.mt.util.bd.Parametro;

public class DataParametros extends DatosTabla {
	private static final String sqlGetId = "SELECT MAX(ID)+1 AS IDX FROM MT.MST_PARAMETROS_GENERALES";
	private static final String sqlFindByPK = "SELECT ID, DESCRIPCION, VALOR, VIGENTE, FECHA FROM MT.MST_PARAMETROS_GENERALES where ID=?";
	private static final String sqlFindAll = "SELECT ID, DESCRIPCION, VALOR, VIGENTE, FECHA FROM MT.MST_PARAMETROS_GENERALES";
	private static final String sqlCreate = "INSERT INTO (ID, DESCRIPCION, VALOR, VIGENTE, FECHA) VALUES (?, ?, ?, ?, ?)";
	private static final String sqlDeleteByPK = "DELETE FROM MT.MST_PARAMETROS_GENERALES where ID=?";


	private DTOParametros setDatosRegistro(ResultSet rs) throws SQLException {
		DTOParametros dto = new DTOParametros();
		dto.setId(formatInteger(rs.getObject("ID")));
		dto.setDescripcion(formatString(rs.getObject("DESCRIPCION")));
		dto.setValor(formatString(rs.getObject("VALOR")));
		dto.setVigente(formatString(rs.getObject("VIGENTE")));
		dto.setFecha(formatDate(rs.getDate("FECHA")));
		return dto;
	}

	public Integer generateId() {
		Parametro[] params = null;
		Integer id = null;
		try {
			getConnection();
			ResultSet rs = executeQuery(sqlGetId, params);
			while (rs.next()) {
				id = formatInteger(rs.getObject("IDX"));
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			closeConnection();
		}
		return id;
	}
	public DTOParametros findByPrimaryKey(Long id) {

		DTOParametros dto = new DTOParametros();
		Parametro[] params = new Parametro[] { getParametro(id, Types.INTEGER)};
		try {
			getConnection();

			ResultSet rs = executeQuery(sqlFindByPK, params);
			while (rs.next()) {
				dto = setDatosRegistro( rs );
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			closeConnection();
		}

		return dto;
	}

	public Collection findAll() {

		Parametro[] params = null;
		Collection col = new LinkedList();
		try {
			getConnection();

			ResultSet rs = executeQuery(sqlFindAll, params);
			while (rs.next()) {
				DTOParametros dto = setDatosRegistro( rs );
				col.add( dto );
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			closeConnection();
		}

		return col;
	}

	public int deleteByPrimaryKey(Integer id) {

		Parametro[] params = new Parametro[] { getParametro(id, Types.INTEGER)};
		int res = -1;
		try {
			getConnection();
			res = executeUpdate(sqlDeleteByPK, params);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			closeConnection();
		}

		return res;
	}

	public int create(DTOParametros dto) {

		Parametro[] params = new Parametro[] {
			getParametro(dto.getId(), Types.INTEGER),
			getParametro(dto.getDescripcion(), Types.VARCHAR),
			getParametro(dto.getValor(), Types.VARCHAR),
			getParametro(dto.getVigente(), Types.VARCHAR),
			getParametro(dto.getFecha(), Types.DATE)
		};

		int res = -1;
		try {
			getConnection();
			res = executeUpdate(sqlCreate, params);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			closeConnection();
		}

		return res;
	}



}

